package com.aaa.service.systemmanage.impl;

import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.dao.systemmanage.UserDao;
import com.aaa.dao.systemmanage.UserRoleDao;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.systemmanage.User;
import com.aaa.service.systemmanage.UserService;
import com.aaa.utils.Password;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ：XiaoxXuan
 * @date ：Created in 2022.08.29 15:24
 * @description：
 * @modified By：
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Autowired
    UserRoleDao userRoleDao;

    @Override
    public User queryUserinfoByUsername(String username) {
        return userDao.queryUserinfoByUsername(username);
    }

    @Override
    public Page queryAll(PageSortEntity pageSortEntity, User user) {
        //分页
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<User> users = userDao.queryAll(user);
        Page pageResult = (Page) users;
        return pageResult;
    }

    @InsertAnn
    @Override
    public boolean insert(User user) {
        String password = user.getPassword();
        //密码加密，并返回盐值和密文密码
        Map<String, String> stringMap = Password.makePasswordSalt(password);
        user.setSalt(stringMap.get("salt"));
        //替换原有的明文密码
        user.setPassword(stringMap.get("password"));
        Integer roleId = user.getRoleId();
        int insert = userDao.insert(user);
        Integer userId = user.getUserid();
        boolean b = userRoleDao.insert(userId, roleId);
        return b;
    }

    @UpdateAnn
    @Override
    public boolean update(User user) {
        return userDao.update(user);
    }

    @Override
    public boolean deleteUser(List list) {
        return userDao.deleteUser(list);
    }


}